package com.elise;

import java.util.Date;

import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

public class EtimePopup extends PopupWindow implements OnClickListener {

	private TextView name;
	private TextView sign;
	private TextView sign2;
	
	private TextView time_in;
	private TextView time_out;
	private TextView minutes;
	private TextView amount;
	
	private Button cancel;
	
	private ViewGroup parent;
		
	//Strict mode seems to ask for a Constructor(Context)
	private EtimePopup(Context context) {}
	
	public EtimePopup(Context context, ViewGroup parent) {
		this(context);
		this.parent = parent;
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.time_detail_popup, parent, false);
		setContentView(view);
		
		setWindowLayoutMode(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		setFocusable(true);
		
		name = (TextView) view.findViewById(R.id.time_detail_popup_name);
		sign = (TextView) view.findViewById(R.id.time_detail_popup_sign);
		sign2 = (TextView) view.findViewById(R.id.time_detail_popup_sign2);
		
		time_in = (TextView) view.findViewById(R.id.time_detail_popup_time_in);
		time_out = (TextView) view.findViewById(R.id.time_detail_popup_time_out);
		minutes = (TextView) view.findViewById(R.id.time_detail_popup_minutes);
		amount = (TextView) view.findViewById(R.id.time_detail_popup_amount);
		
		cancel = (Button) view.findViewById(R.id.btn_popup_cancel);
		cancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		
			case R.id.btn_popup_cancel:
				dismiss();
				break;
		}
	}
	
	public void show(Etime etime) {
		name.setText(etime.getName());
		sign.setText(Etime.signs[etime.getSign()]);
		sign2.setText(Etime.signs[etime.getSign2()]);
		
		time_in.setText(Etime.timeFormat.format(etime.getTime_in()));
		time_out.setText(Etime.timeFormat.format(etime.getTime_out()));
		
		etime.setMinutes(new Date(etime.getTime_out().getTime() - etime.getTime_in().getTime()));
		minutes.setText(Etime.timeFormat.format(etime.getMinutes()));
		
		etime.setAmount(etime.getMinutes().getTime()/60000d*.17);
		amount.setText(Etime.currencyFormat.format(etime.getAmount()));
		
		showAtLocation(parent, Gravity.CENTER, 0, 0);
	}
}